import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:qq_music/tools/NetWorkController.dart';
import 'package:qq_music/tools/Page/Home/Bottom.dart';
import 'package:qq_music/tools/Page/Home/function/ListSond/ToneQuality.dart';
import 'package:qq_music/tools/PlayController.dart';
import 'package:qq_music/tools/extends/Json/SelectSond/QQ_music/select_sond.dart' as select_sond;
import 'dart:convert' as convert;

// final key = GlobalKey<MyStatefulWidgetOneState>();
final key = GlobalKey<_GetSondListState>();

class MyStatefulWidgetOne extends StatefulWidget {
  MyStatefulWidgetOne({Key key}) : super(key: key);
  MyStatefulWidgetOneState createState() => MyStatefulWidgetOneState();
}

class MyStatefulWidgetOneState extends State<MyStatefulWidgetOne> {
  String _message = "Hello world!";
  String get message => _message;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          Container(
            height: 20.0,
          ),
          Text('Widget one message: ' + _message)
        ],
      ),
    );
  }

  // void netWorkSelectSond(String msg) {
  //   setState(() {
  //     _message = msg;
  //   });
  // }
}

Widget songJson = Container(
  // padding: const EdgeInsets.all(),
  child: new ListView.builder(
    itemCount: 30,
    itemBuilder: (context, index) {
      return new ListTile(
        title: new Text('$index'),
      );
      // child: new Row(
      //   children: [
      //     FlatButton(
      //       child: Text('aaaa'),
      //       onPressed: () {},
      //     )
      //   ],
      // ),
    },
  ),
);

class GetSondList extends StatefulWidget {
  GetSondList({Key key}) : super(key: key);
  State<StatefulWidget> createState() {
    return _GetSondListState();
  }
}

class _GetSondListState extends State<GetSondList> {
  // List<dynamic> json = [
  //   {'我是': '人'}
  // ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(980.0),
      child: songJson,
    );
  }

  netWorkSelectSond(String songName) {
    NetWorkController.get(
        'http://c.y.qq.com/soso/fcgi-bin/client_search_cp?format=json&t=0&loginUin=0&inCharset=GB2312&outCharset=utf-8&qqmusic_guid=84668892142273b824b454b60a103c68b874b943&qqmusic_ver=60200&ct=6&catZhida=1&p=1&n=30&searchid=5AF3ABC6-70F8-4086-9EBD-1558756E889A31680&w=$songName&flag_qc=0&remoteplace=txt.mac.search&new_json=1&lossless=0&aggr=0&cr=1&sem=0',
        (data) {
      // print(data['code']);
      Map<String, dynamic> user = convert.jsonDecode(data);
      var sonList = select_sond.SelectSond.fromJson(user).data.song;
      // int sondListNumber = sonList.curnum;
      // var sondList = select_sond.List.fromJson(sonList.list[0]);
      // print(sondListNumber);
      // print(sondList.toJson());

      // select_sond.SelectSond selectSond = data;
      // selectSond.data.toJson();
      // Map<String, dynamic> srcJson = select_sond /* .Data.fromJson() */;
      // print(srcJson);
      /* SelectSond sondList = */
      setState(() {
        songJson = loginSondList(context, sonList.list);
        /* var loginSondListContext = context; */
        /* var loginSondListList = sonList.list; */
      });
    }, parameter: {'data1': 'sdsada'});
  }

  loginSondList(BuildContext context, List<dynamic> list, {int clickColorsList}) {
    Map itemColors = new Map();
    return Container(
      child: Container(
        margin: const EdgeInsets.only(
          top: 10.0,
          left: 10.0,
          right: 10.0,
        ),
        child: ListView.builder(
          itemCount: list.length,
          itemBuilder: (BuildContext context, int index) {
            // print(index);
            if (clickColorsList == index) {
              itemColors['$index'] = Colors.blue;
            } else {
              itemColors['$index'] = null;
            }
            var songInfo = select_sond.List.fromJson(list[index]);
            // print(songInfo.toJson());
            return InkWell(
              splashColor: Colors.yellow,
              onTap: () {
                String fileExtendsName = ToneQuality().descendingOrder(songInfo.file);
                PlayController.playSond(songInfo.mid, songInfo.file.mediaMid, fileExtendsName, (data) {
                  print(data);
                });
                /* setState(() {
                  itemColors['$index'] = Colors.blue;
                  songJson = songJson;
                }); */
                sondListClick(index, list[index]);
                PlayController.getTotalLength((data) {
                  playStateState.currentState.getPlaybackProgress(data);
                });
                setState(() {
                  playState = Icon(Icons.pause);
                  songJson = loginSondList(context, list, clickColorsList: index);
                });
              },
              child: new Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new Container(
                    // 上面的大字
                    margin: const EdgeInsets.only(top: 2.0),
                    child: SingleChildScrollView(
                      child: Container(
                        height: 24.0,
                        child: ListView(
                          scrollDirection: Axis.horizontal,
                          children: List.generate(songInfo.singer.length, (i) {
                            return Text(
                              songInfo.title,
                              overflow: TextOverflow.ellipsis,
                              style: new TextStyle(fontSize: 21.0, color: itemColors['$index']),
                            );
                          }),
                        ),
                      ),
                    ),
                  ),
                  Row(
                    children: <Widget>[
                      songDataFileImages(songInfo.file.toJson()),
                      mvDataFileImages(songInfo.toJson()),
                      new Container(
                        // 下面的小字
                        margin: const EdgeInsets.only(top: 8.0, left: 5.0),
                        child: SingleChildScrollView(
                          child: Container(
                              height: 18.0,
                              width: 271.0,
                              /* color: Colors.black12, */
                              child: ListView(
                                scrollDirection: Axis.horizontal,
                                children: List.generate(songInfo.singer.length, (i) {
                                  if (songInfo.singer.length == 0) {
                                    return Padding(
                                      child: Text(
                                        select_sond.Singer.fromJson(songInfo.singer[i]).name.toString(),
                                        style: TextStyle(color: itemColors['$index']),
                                      ),
                                      padding: new EdgeInsets.only(),
                                    );
                                  } else {
                                    if (i == songInfo.singer.length) {
                                      return Padding(
                                        child: Text(
                                          select_sond.Singer.fromJson(songInfo.singer[i]).name.toString(),
                                          style: TextStyle(color: itemColors['$index']),
                                        ),
                                        padding: new EdgeInsets.only(),
                                      );
                                    } else {
                                      if (i == 0) {
                                        return Padding(
                                          child: Text(
                                            select_sond.Singer.fromJson(songInfo.singer[i]).name.toString(),
                                            style: TextStyle(color: itemColors['$index']),
                                          ),
                                          padding: new EdgeInsets.only(left: 0.0),
                                        );
                                      } else {
                                        return Padding(
                                          child: Row(
                                            children: <Widget>[
                                              Padding(
                                                padding: new EdgeInsets.only(left: 8.0, right: 8.0),
                                                child: Text(
                                                  '·',
                                                  style: TextStyle(color: itemColors['$index']),
                                                ),
                                              ),
                                              Padding(
                                                padding: new EdgeInsets.only(),
                                                child: Text(
                                                  select_sond.Singer.fromJson(songInfo.singer[i]).name.toString(),
                                                  style: TextStyle(color: itemColors['$index']),
                                                ),
                                              ),
                                            ],
                                          ),
                                          padding: new EdgeInsets.only(),
                                        );
                                      }
                                    }
                                  }
                                }),
                              )),
                        ),
                      ),
                    ],
                  )
                ],
              ),
            );
          },
        ),
      ),
    );
    //print(list.length);
    // new ListView.builder(
    //   itemCount: list.length,
    //   itemBuilder: (BuildContext context, int index) {
    //     print('$index');
    //     return new Text('aaa');
    //   },
    // );
  }

  Widget songDataFileImages(var songDataFileImage) {
    //生成音乐 sq hq 图标的
    Widget iconImage;
    if (songDataFileImage['size_flac'] != 0) {
      //无损
      iconImage = Container(
        margin: const EdgeInsets.only(top: 8.0),
        child: SingleChildScrollView(
          /*  */
          child: new Image.asset(
            'images/minIcon/song_sq/sq_play.png',
            height: 16.0,
          ),
        ),
      );
    } else {
      if (songDataFileImage['size_ogg'] != 0) {
        //高清
        iconImage = Container(
          margin: const EdgeInsets.only(top: 8.0),
          child: SingleChildScrollView(
            /*  */
            child: new Image.asset(
              'images/minIcon/song_hq/hq_play.png',
              height: 16.0,
            ),
          ),
        );
      } else {
        if (songDataFileImage['size_ape'] != 0) {
          //无损
          iconImage = Container(
            margin: const EdgeInsets.only(top: 8.0),
            child: SingleChildScrollView(
              /*  */
              child: new Image.asset(
                'images/minIcon/song_sq/sq_play.png',
                height: 16.0,
              ),
            ),
          );
        } else {
          if (songDataFileImage['size_aac'] != 0) {
            //高清
            iconImage = Container(
              margin: const EdgeInsets.only(top: 8.0),
              child: SingleChildScrollView(
                /*  */
                child: new Image.asset(
                  'images/minIcon/song_hq/hq_play.png',
                  height: 16.0,
                ),
              ),
            );
          } else {
            if (songDataFileImage['size_320mp3'] != 0) {
              //高清
              iconImage = Container(
                margin: const EdgeInsets.only(top: 8.0),
                child: SingleChildScrollView(
                  /*  */
                  child: new Image.asset(
                    'images/minIcon/song_hq/hq_play.png',
                    height: 16.0,
                  ),
                ),
              );
            } else {
              iconImage = Container(
                margin: const EdgeInsets.only(top: 8.0),
                child: SingleChildScrollView(
                  /*  */
                  child: null,
                ),
              );
            }
          }
        }
      }
    }
    return iconImage;
  }

  Widget mvDataFileImages(Map<String, dynamic> mvDataFileJson) {
    if (mvDataFileJson['mv'].vid != '') {
      return Container(
        margin: const EdgeInsets.only(top: 8.0, left: 3.0),
        child: SingleChildScrollView(
          /*  */
          child: new Image.asset(
            'images/minIcon/mv/cell_mv.png',
            height: 16.0,
          ),
        ),
      );
    }
    return Container(
      margin: const EdgeInsets.only(top: 8.0),
      child: SingleChildScrollView(
        /*  */
        child: null,
      ),
    );
  }
  // buildListView(BuildContext context, select_sond.List<Data> list) {
  //   print(list[0].song.name);
  //   // return new ListView.builder(
  //   //   itemBuilder: (BuildContext context, int index) {
  //   //     // print('$list[0]');
  //   //     // return Text('$list[$index].name');
  //   //     return new Container(
  //   //         // padding: const EdgeInsets.all(32.0),
  //   //         child: new Row(
  //   //       children: [
  //   //         new Image.network(
  //   //           '${list[index].cover}',
  //   //           fit: BoxFit.cover,
  //   //           width: ScreenUtil.getInstance().setWidth(300),
  //   //           height: ScreenUtil.getInstance().setWidth(300),
  //   //         ),
  //   //         new Expanded(
  //   //           child: new Column(
  //   //             crossAxisAlignment: CrossAxisAlignment.start,
  //   //             children: [
  //   //               new Container(
  //   //                 padding: const EdgeInsets.only(bottom: 8.0),
  //   //                 child: new Text(
  //   //                   list[index].name,
  //   //                   style: new TextStyle(
  //   //                     fontWeight: FontWeight.bold,
  //   //                   ),
  //   //                 ),
  //   //               ),
  //   //               new Text(
  //   //                 '大小' +
  //   //                     simpleCalculation(simpleCalculation(double.parse(list[index].size), 1024, '/'), 1024, '/')
  //   //                         .toStringAsFixed(2)
  //   //                         .toString() +
  //   //                     'Mb' +
  //   //                     '歌手' +
  //   //                     list[index].singer,
  //   //                 style: new TextStyle(
  //   //                   color: Colors.grey[500],
  //   //                 ),
  //   //               ),
  //   //             ],
  //   //           ),
  //   //         ),
  //   //       ],
  //   //     ));
  //   //   },
  //   //   itemCount: list.length,
  //   // );
  // }
}

////** Undocumented simpleCalculation
///  *
///  * @param [int] index 被点击的索引
///  * @return void
///*/
sondListClick(int index, Map<String, dynamic> map) {
  var songInfo = select_sond.List.fromJson(map);
  // print(songInfo.title);
  // bvs.currentState.bottomIconTitleEdit();
  // bottomIconTitleEdit();
  bvs.currentState.bottomIconTitleEdit(songInfo.album, songInfo.title);
}
